<?php

/**
 * 文档信息仓库
 * @author wangwx 2015-5-12
 */

namespace common\repositories\myhtsq;

use common\repositories\BaseRepository;
use common\support\StringHelper;
use common\entities\myhtsq\DocumentEntity;
use common\error\ErrorCode;
use common\models\myhtsq\DocumentModel;

class DocumentRepository extends BaseRepository {

    /**
     * 构造方法
     * @param unknown $config (optional)
     */
    public function __construct($config = []) {
        parent::__construct($config);
    }

    /**
     * 文档列表
     * @param string $businessId 业务id
     * @return array
     */
    public static function getDocumentListByBusinessId($businessId) {
        return DocumentEntity::find()
                        ->where(['business_id' => $businessId])
                        ->orderBy(['document_name' => SORT_ASC])
                        ->asArray()
                        ->all();
    }

    /**
     * 新增文档
     * @param \common\models\myhtsq\DocumentModel $documentModel
     * @return boolean|string 失败返回false，成功返回新增的文档Id
     */
    public function addDocument($documentModel) {
        try {
            $entity = new DocumentEntity();
            $entity->setAttributes($documentModel->attributes);
            $entity->document_id = \common\support\StringHelper::uuid();
            $result = $entity->save();
            if ($result === FALSE) {
                $error = $entity->firstErrorMessage;
                return $this->setAndReturn(ErrorCode::ERR_DOCUMENT_SAVE_FAIL, $error);
            } else {
                return $entity->primaryKey;
            }
        } catch (\Exception $ex) {
            return $this->setAndReturn(ErrorCode::ERR_DOCUMENT_SAVE_FAIL, (string) $ex);
        }
    }

    /**
     * 根据文件id,删除文件记录
     * @param string $businessId 业务id
     * @param string $documentId 文件id
     * @return boolean
     */
    public static function delDocumentById($docmentId) {
        return DocumentEntity::deleteAll(['document_id' => $docmentId]);
    }

    /**
     * 根据业务id删除文件记录
     * @param string $businessId 业务id
     * @return boolean
     */
    public static function delDocumentByBusinessId($businessId) {
        return DocumentEntity::deleteAll(['business_id' => $businessId]);
    }

    /**
     * 
     * @param $document
     * @param $platform
     * @return bool|mixed
     */
    public static function addDocumentForm($document,$platform = 1){
        if($platform == 1){
            $entity = new DocumentEntity();
            $modelInfo = $entity::find()->where(['document_id' => $document['document_id']])->one();
            if($modelInfo){
                $modelInfo->setAttributes($document);
                if ($modelInfo->save()) {
                    return $modelInfo->document_id;
                } else {
                    return false;
                }
            }else{
                $entity->setAttributes($document);
                if($entity->save()) {
                    return $entity->primaryKey;
                }else{
                    return false;
                }
            }
        }
    }

}
